package com.ideaaedi.zoo.example.springboot.seata.alpha.config;

import com.ideaaedi.commonspring.transaction.DefaultManualTransactionSupport;
import com.ideaaedi.zoo.diy.artifact.seata.transaction.SeataManualTransactionSupport;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.PostConstruct;
import java.util.Objects;

/**
 * 指定创建seate手动事务时，默认的本地事务获取方式
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2022/3/25 10:33
 */
@Slf4j
@Configuration
public class ManualTransactionConfig {
    
    @Resource
    private DataSourceTransactionManager defaultTransactionManager;
    
    @PostConstruct
    public void initTransactionManager() {
        // 本地事务手动管理器
        DefaultManualTransactionSupport.initTransactionManager(Objects.requireNonNull(defaultTransactionManager));
        
        // 设置：supply本地事务手动管理器（每次都new，不要用同一个对象）
        SeataManualTransactionSupport.initLocalTransactionSupportProvider(DefaultManualTransactionSupport::create);
    }
}
